package com.itheima.controller;

import com.itheima.domain.Teacher;
import com.itheima.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

/*
老师用户交互类 (人机交互  键盘录入  输出语句)
成员变量  service    Scanner 对象
构造方法  默认无参
成员方法  入口
        增
        删
        改
        查

 */
public class TeacherController {
    private TeacherService service= new TeacherService();
    private Scanner  sc=new Scanner(System.in);


    //入口
    public  void start(){
        Scanner sc = new Scanner(System.in);
        //死循环
        loop:
        while (true) {
            // 键盘录入
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            //根据不同的选择做不同的事
            switch (choice) {
                case "1":
                    //System.out.println("添加成功");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除成功");
                    deleteTeacher();
                    break;
                case "3":
                    // System.out.println("修改成功");
                    updateTeacher();
                    break;
                case "4":
                    // System.out.println("查看成功");
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("退出老师系统");
                    // break loop;
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }
    //增
    public void addTeacher(){
        String sid = null;
        while (true) {
            System.out.println("请录入工号");
            sid = sc.next();
            boolean exists = service.isExists(sid);
            if(exists){
                System.out.println("已经存在,请重新输入");
            }else {
                break;
            }
        }
        Teacher teacher = getTeacherInfo(sid);
        service.addTeacher(teacher);
        System.out.println("添加成功");
    }

    public Teacher getTeacherInfo(String sid) {
        System.out.println("请录入姓名");
        String name = sc.next();
        System.out.println("请录入年龄");
        int age = sc.nextInt();
        System.out.println("请录入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }

    //        删
    public  void deleteTeacher(){
        ArrayList<Teacher> list = service.findAllTeachers();
        if(list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = getTeacherSid();
        service.deleteTeacher(sid);
        System.out.println("删除成功");
    }

    public String getTeacherSid() {

        String sid = null;
        while (true) {
            System.out.println("请录入工号");
            sid = sc.next();
            boolean exists = service.isExists(sid);
            if(exists){
                break;
            }else {
                System.out.println("已经存在,请重新输入");
            }
        }
        return sid;
    }

    //        改
    public  void updateTeacher(){
        ArrayList<Teacher> list = service.findAllTeachers();
        if(list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = getTeacherSid();
        Teacher teacher = getTeacherInfo(sid);
        service.updateTeacher(sid,teacher);
        System.out.println("修改成功");
    }
    //        查
    public  void findAllTeachers(){
        ArrayList<Teacher> list = service.findAllTeachers();
        if(list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        System.out.println(list);

    }

}
